$(function () {
	let url = '../orderdetail/list';
	let orderno = getQueryString("orderNo");
	if (orderno) {
		url+='?orderno='+orderno;
	}
    $("#jqGrid").Grid({
        url: url,
        colModel: [
			{label: '订单编号', name: 'orderNo', index: 'order_no', key: true, width: 90,align:'center'},
			{label: '产品名称', name: 'proName', index: 'pro_name', width: 80,align:'center'},
			{label: '产品图', name: 'proUrl', index: 'pro_url', width: 45,align:'center',
				formatter:function (value) {
					if(value!=""){
						return transImg(value);
					}else{
						return "无图片";
					}
				}
			},
			{label: '款式名称', name: 'styleName', index: 'style_name', width: 50,align:'center'},
			{label: '款式图', name: 'styleUrl', index: 'style_url', width: 45,align:'center',
				formatter:function (value) {
					if(value!=""){
						return transImg(value);
					}else{
						return "无图片";
					}
				}
			},
			{label: '数量', name: 'num', index: 'num', width: 30,align:'center'},
			{label: '价格', name: 'price', index: 'price', width: 30,align:'center'},
			{label: '服务费', name: 'rateValue', index: 'rateValue', width: 30,align:'center'},
			{label: '赠送SPC能量', name: 'giveEnergy', index: 'giveEnergy', width: 30,align:'center'},
			{label: '释放能量SPC', name: 'spcFree', index: 'spcFree', width: 30,align:'center'},
			{label: '团队赠送SPC', name: 'giveTeam', index: 'giveTeam', width: 30,align:'center'},
			{label: '产品描述', name: 'description', index: 'description', width: 80,align:'center'},
			{label: '产品类型', name: 'type', index: 'type', width: 80,align:'center',
				formatter: function (value) {
					if (value == 1) {
						return '<span class="label-success" style="padding: 5px">价格产品</span>';
					}
					if (value == 2) {
						return '<span class="label-warning" style="padding: 5px">兑换产品</span>';
					}
					if (value == 3) {
						return '<span class="label-info" style="padding: 5px">余额兑换产品</span>';
					}
					if (value == 4) {
						return '<span class="label-info" style="padding: 5px">D-one产品</span>';
					}
					return '<span class="label-error">其他</span>';
				}
			},
			{label: '备注', name: 'remark', index: 'remark', width: 80,align:'center'}
		]
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		isSave:true,
		tOrderDetail: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
			orderno: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.isSave = true;
			vm.title = "新增";
			vm.tOrderDetail = {};
		},
		update: function (event) {
            let orderNo = getSelectedRow("#jqGrid");
			if (orderNo == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";
			vm.isSave = false;
            vm.getInfo(orderNo)
		},
		saveOrUpdate: function (event) {
            let url = vm.isSave ? "../orderdetail/save" : "../orderdetail/update";
            Ajax.request({
			    url: url,
                params: JSON.stringify(vm.tOrderDetail),
                type: "POST",
			    contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
			});
		},
		del: function (event) {
            let orderNos = getSelectedRows("#jqGrid");
			if (orderNos == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
                Ajax.request({
				    url: "../orderdetail/delete",
                    params: JSON.stringify(orderNos),
                    type: "POST",
				    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
					}
				});
			});
		},
		getInfo: function(orderNo){
            Ajax.request({
                url: "../orderdetail/info/"+orderNo,
                async: true,
                successCallback: function (r) {
                    vm.tOrderDetail = r.tOrderDetail;
                }
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.isSave = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'orderno': vm.q.orderno},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        reloadSearch: function() {
            vm.q = {
				orderno: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});